package com.longy.gulimall.product.mq;

import com.longy.gulimall.product.controller.AmtrespLi;
import com.sun.jndi.ldap.pool.PooledConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;
import org.springframework.jms.connection.SingleConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.support.converter.MappingJackson2MessageConverter;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jms.support.converter.MessageType;
import org.springframework.util.backoff.FixedBackOff;

import javax.jms.*;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class JmsConfig  {

    @Bean("queueListener")
    public JmsListenerContainerFactory<?> queueJmsListenerContainerFactory(ConnectionFactory connectionFactory, MessageConverter messageConverter) {
        SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPubSubDomain(false);
        factory.setMessageConverter(messageConverter);
        return factory;
    }

    /**
     * 在Topic模式中，对消息的监听需要对containerFactory进行配置
     */
    @Bean("topicListener")
    public JmsListenerContainerFactory<?> topicJmsListenerContainerFactory(SingleConnectionFactory  connectionFactory, DefaultJmsListenerContainerFactoryConfigurer configurer , MessageConverter messageConverter) throws JMSException {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        connectionFactory.setClientId("client_lmc1");
        factory.setConnectionFactory(connectionFactory);
        factory.setPubSubDomain(true);
        factory.setSubscriptionDurable(true);
        factory.setConcurrency("1");
        factory.setMessageConverter(messageConverter);
        return factory;
    }

    @Bean
    public MessageConverter messageConverter() {
        JsonMessageConverter converter = new JsonMessageConverter();
//        Map<String, Class<?>> typeIdMappings = new HashMap<>();
//        typeIdMappings.put("com.longy.gulimall.product.controller.Amtresp", AmtrespLi.class);
//        converter.setTypeIdMappings(typeIdMappings);
//        converter.setTargetType(MessageType.TEXT);
//        converter.setTypeIdPropertyName("_type");
        return converter;
    }

    @Bean
    public Queue testQueue() {
        return new ActiveMQQueue("test.queue");
    }

    @Bean
    public Queue devQueue() {
        return new ActiveMQQueue("dev_queue");
    }

    @Bean
    public Queue sitQueue() {
        return new ActiveMQQueue("sitQueue");
    }

    @Bean(name = "topic")
    public Topic topic() {
        return new ActiveMQTopic("topicQueue");
    }


}
